﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Google.Protobuf;
using UnityEngine.UI;

public enum EServer
{
    Local,
    Remote,
}

public class Launcher : MonoBehaviour
{
    public static Launcher Instance { private set; get; }

    [SerializeField] UnityEngine.UI.Text m_UserID;
    [SerializeField] bool m_Offline;
    [SerializeField] string m_IP;
    [SerializeField] EServer m_Server;
    [SerializeField] InputField m_Input;
    [SerializeField] bool m_NeedInputIP;
    [SerializeField] GameObject m_HideAfterRun;
    [SerializeField] GameObject m_ShowAfterRun;

    [SerializeField] GameObject m_UILog;
    [SerializeField] Text m_LogText;
    const int MAX_LOG_LINES = 100;
    List<string> m_LogString = new List<string>();
    public MoveComp m_MoveLeft;
    public MoveComp m_MoveRight;
    public Client m_Client;
    [SerializeField] Player m_OfflineLocalPlayer;

    private void Awake()
    {
        Instance = this;
        m_Client = new Client();
        Screen.orientation = ScreenOrientation.LandscapeLeft;
        Screen.autorotateToLandscapeLeft = false;
        Screen.autorotateToLandscapeRight = false;
        Screen.autorotateToPortrait = false;
        Screen.autorotateToPortraitUpsideDown = false;
    }

    public void OnClickOpenLog()
    {
        m_UILog.SetActive(true);
        string ss = "";
        foreach (var item in m_LogString)
        {
            ss += item + "\n";
        }
        m_LogText.text = ss;
    }

    public void OnClickCloseLog()
    {
        m_LogText.text = null;
        m_UILog.SetActive(false);
    }

    public void AddLog(string s)
    {
        m_LogString.Add(s);
        if (m_LogString.Count > MAX_LOG_LINES)
        {
            m_LogString.RemoveAt(0);
        }
    }

    public void OnClickOK()
    {
        if (!m_NeedInputIP)
        {
            return;
        }
        if (m_Input.text != "")
        {
            m_IP = m_Input.text;
        }
        PlayerPrefs.SetString("ip", m_IP);
        OnStart();
    }

    void OnLog(string condition, string stackTrace, LogType type)
    {
        AddLog(condition);
    }

    void Start()
    {
        OnClickCloseLog();
        Application.logMessageReceived += OnLog;
        if (m_Server == EServer.Local)
        {
            m_IP = "127.0.0.1:1234";
        }
        else
        {
            m_IP = "119.45.15.219:1234";
        }

        m_Input.text = m_IP;
        if (PlayerPrefs.HasKey("ip"))
        {
            m_Input.text = PlayerPrefs.GetString("ip");
        }

        m_OfflineLocalPlayer.gameObject.SetActive(false);
        m_ShowAfterRun.SetActive(false);
        if (!m_NeedInputIP)
        {
            OnStart();
        }
    }

    public void Stop()
    {
        m_Client = new Client();
        m_ShowAfterRun.SetActive(false);
        m_HideAfterRun.SetActive(true);
    }

    void OnStart()
    {
        m_ShowAfterRun.SetActive(true);
        m_HideAfterRun.SetActive(false);

        if (m_Offline)
        {
            m_OfflineLocalPlayer.gameObject.SetActive(true);
            m_OfflineLocalPlayer.transform.rotation = Quaternion.Euler(0, 30, 0);
            Client.Instance.m_Players.Add(0, m_OfflineLocalPlayer);
            //Client.Instance.FindAndCreatePlayer(0, Vector3.zero);
            m_UserID.gameObject.SetActive(false);
        }

        if (!m_Offline)
        {
            m_OfflineLocalPlayer.gameObject.SetActive(false);
            var ss = m_IP.Split(':');
            Client.Instance.m_IP = ss[0];
            Client.Instance.m_Port = int.Parse(ss[1]);
            Client.Instance.Start();
        }
    }

    void Update()
    {
        if (m_Offline)
        {
            foreach (var item in Client.Instance.m_Players)
            {
                item.Value.Tick();
            }
        }

        if (!m_Offline)
        {
            Client.Instance.Tick();
            m_UserID.text = "UserID: " + Client.Instance.m_PlayerID + " DeltaTickCount: " + Client.Instance.DeltaTickCount;
        }
    }

    private void OnDestroy()
    {
        if (!m_Offline)
        {
            Client.Instance.Stop();
        }
        LogManager.FileStream.Close();
        LogManager.FileStream.Dispose();
    }

    public void OnClickResend()
    {
        Client.Instance.ResendTest();
    }

    public void OnClickOrder()
    {
        Client.Instance.OrderTest();
    }
}
